home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15302 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  70 lines

  1. Path: mdw084.cc.monash.edu.au!pdrod1
  2. From: pdrod1@mdw084.cc.monash.edu.au (Mr Paul Rodger)
  3. Newsgroups: comp.lang.c
  4. Subject: passing 2D arrays to functions
  5. Date: 18 Apr 1996 11:26:12 GMT
  6. Organization: Monash University
  7. Message-ID: <4l58sk$l6r@harbinger.cc.monash.edu.au>
  8. NNTP-Posting-Host: mdw084.cc.monash.edu.au
  9. X-NNTP-Posting-User: pdrod1
  10. X-Newsreader: NN version 6.5.0 CURRENT #12
  11.  
  12. Greets - 
  13.  
  14. I need to pass a 2-dimensional array to a function. Obviously I just want
  15. to pass a pointer to the first element, and in the past when I wanted to 
  16. pass a one-dimensional array to a function I did the following which should 
  17. work fine:
  18.  
  19. fun(int array[])
  20. {
  21. ...
  22. }
  23.  
  24. main()
  25. {
  26.     int array[10];
  27. ...
  28.     fun(array);
  29. }
  30.  
  31. But when I try to pass a 2D array using the same method, slightly different:
  32.  
  33. fun(int array[][])
  34. {
  35. ...
  36. }
  37.  
  38. main()
  39. {
  40.         int array[10][10];
  41. ...
  42.         fun(array);
  43. }
  44.  
  45. I'm getting errors:
  46.  
  47. main.c:221: arithmetic on pointer to an incomplete type
  48. make: *** [main.o] Error 1
  49.  
  50. This occurs when I try to look at elements in the array when I am
  51. in the 'fun' function:
  52.  
  53. array[1][1] = 1;
  54.  
  55. Would this be because of vagueness about row-major or column-major form?
  56. I've got around this in the past by putting the 2D array in a struct,
  57. and passing a pointer to a struct, but in this case I can't do that 
  58. because the array size could vary as it is declared various times in 
  59. iterations. (It isn't declared in main like above - this is just a 
  60. simplication).
  61.  
  62. I'm using gcc 2.7.0 for linux, but had this problem before with BCPP.
  63.  
  64. Any help would be great.. :)
  65. -- 
  66.   ._____________________________________________________________________.
  67.   |        "We all get a little crazy sometimes" - Norman Bates         |
  68.   \_____________________________________________________________________/ 
  69.  
  70.